package cn.xzqwjw.taskmanager.controller.api;

import cn.xzqwjw.taskmanager.domain.pojo.SysPurviewRoute;
import cn.xzqwjw.taskmanager.service.SysPurviewRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author rush
 */
@RestController
@RequestMapping("/api/purview-route")
@Validated
public class SysPurviewRouteController {

  private final SysPurviewRouteService sysPurviewRouteService;

  @Autowired
  public SysPurviewRouteController(SysPurviewRouteService sysPurviewRouteService) {
    this.sysPurviewRouteService = sysPurviewRouteService;
  }

  @PostMapping("")
  public String add(
      @NotNull(message = "参数不能为空")
      @Valid @RequestBody SysPurviewRoute sysPurviewRoute) {
    return sysPurviewRouteService.add(sysPurviewRoute);
  }

  /**
   * 处理 "/api/purview-route" 的 PATCH 请求，修改某权限对应的路由
   */
  @PatchMapping("")
  public String modifyRouteIdByPurviewId(
      @NotNull(message = "PurviewId不能为空")
      @Min(value = 1, message = "PurviewId必须大于0")
      @RequestParam(value = "PurviewId") Integer idPurview,
      @NotNull(message = "RouteId不能为空")
      @Min(value = 1, message = "RouteId必须大于0")
      @RequestParam(value = "RouteId") Integer idRoute) {
    return sysPurviewRouteService.modifyRouteIdByPurviewId(idPurview, idRoute);
  }

  @GetMapping("")
  public String queryRouteIdByPurviewId(
      @NotNull(message = "PurviewId不能为空")
      @Min(value = 1, message = "PurviewId必须大于0")
      @RequestParam(value = "PurviewId") Integer idPurview) {
    return sysPurviewRouteService.getRouteIdByPurviewId(idPurview);
  }

}
